package com.android.djlmapp.adapter.kaika;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;

import com.android.djlmapp.R;
import com.android.djlmapp.model.kaika.SelectNumInfo;
import com.android.djlmapp.tools.other.TextUtil;

import org.refresh_view.pullrecyclerview.BaseRecyclerAdapter;
import org.refresh_view.pullrecyclerview.BaseViewHolder;

import java.util.ArrayList;

/**
 * 宽带套餐数据适配器
 * 2017/12/12.
 */

public class SelectNumAdapter extends BaseRecyclerAdapter<SelectNumInfo.PhoneList> {

    public SelectNumAdapter(Context context) {
        super(context, R.layout.item_selectnum_layout, new ArrayList<SelectNumInfo.PhoneList>());
    }

    @Override
    protected void convert(BaseViewHolder holder, SelectNumInfo.PhoneList item) {
        holder.setText(R.id.tv_select_bq1, "");
        holder.setText(R.id.tv_select_bq2, "");
        holder.setText(R.id.tv_select_bq3, "");
        holder.setText(R.id.tv_select_bq4, "");

        String phonenum = item.getPhonenum(); // 号码
        String price = item.getPrice(); // 价格
        String region = item.getRegion(); // 地址
        ArrayList<String> key_words = item.getKey_words();

        holder.setText(R.id.tv_select_address, region);
        holder.setText(R.id.tv_select_price, "¥:" + price);
        String strPhoneNum = TextUtil.separateString(phonenum, 3, 4, '-');
        if (strPhoneNum.length() > 3) {
            //部分文字改变颜色
            ForegroundColorSpan redSpan = new ForegroundColorSpan(mContext.getResources().getColor(R.color.homeTextRead));
            //这里注意一定要先给textview赋值
            SpannableStringBuilder builder = new SpannableStringBuilder(strPhoneNum);
            //为不同位置字符串设置不同颜色
            builder.setSpan(redSpan, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            //最后为textview赋值
            holder.setText(R.id.tv_select_num, builder);
        }


        switch (key_words.size()) {
            case 0:
                holder.setVisible(R.id.tv_select_bq1, false);
                holder.setVisible(R.id.tv_select_bq2, false);
                holder.setVisible(R.id.tv_select_bq3, false);
                holder.setVisible(R.id.tv_select_bq4, false);
                break;
            case 1:
                holder.setVisible(R.id.tv_select_bq1, true);
                holder.setVisible(R.id.tv_select_bq2, false);
                holder.setVisible(R.id.tv_select_bq3, false);
                holder.setVisible(R.id.tv_select_bq4, false);
                holder.setText(R.id.tv_select_bq1, key_words.get(0));
                break;
            case 2:
                holder.setVisible(R.id.tv_select_bq1, true);
                holder.setVisible(R.id.tv_select_bq2, true);
                holder.setVisible(R.id.tv_select_bq3, false);
                holder.setVisible(R.id.tv_select_bq4, false);
                holder.setText(R.id.tv_select_bq1, key_words.get(0));
                holder.setText(R.id.tv_select_bq2, key_words.get(1));
                break;
            case 3:
                holder.setVisible(R.id.tv_select_bq1, true);
                holder.setVisible(R.id.tv_select_bq2, true);
                holder.setVisible(R.id.tv_select_bq3, true);
                holder.setVisible(R.id.tv_select_bq4, false);
                holder.setText(R.id.tv_select_bq1, key_words.get(0));
                holder.setText(R.id.tv_select_bq2, key_words.get(1));
                holder.setText(R.id.tv_select_bq3, key_words.get(2));
                break;
            case 4:
                holder.setVisible(R.id.tv_select_bq1, true);
                holder.setVisible(R.id.tv_select_bq2, true);
                holder.setVisible(R.id.tv_select_bq3, true);
                holder.setVisible(R.id.tv_select_bq4, true);
                holder.setText(R.id.tv_select_bq1, key_words.get(0));
                holder.setText(R.id.tv_select_bq2, key_words.get(1));
                holder.setText(R.id.tv_select_bq3, key_words.get(2));
                holder.setText(R.id.tv_select_bq4, key_words.get(3));
                break;
        }

        holder.setOnClickListener(R.id.ll_selectnum, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onItemClick(item);
            }
        });


    }


    public OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(SelectNumInfo.PhoneList item);
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickListener = mOnItemClickLitener;
    }

}
